﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
namespace System.Net.Http.Formatting
{
    public class ImageMediaTypeFormatter : BufferedMediaTypeFormatter
    {
        public ImageMediaTypeFormatter()
        {
            this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("image/png"));
        }
        public override MediaTypeFormatter GetPerRequestFormatterInstance(Type type, HttpRequestMessage request, MediaTypeHeaderValue mediaType)
        {
            var mediaTypeFormatter = base.GetPerRequestFormatterInstance(type, request, mediaType);
            return mediaTypeFormatter;
        }

        public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
        {
            base.SetDefaultContentHeaders(type, headers, mediaType);
        }
        public override void WriteToStream(Type type, object value, Stream writeStream, HttpContent content)
        {
            var stream = value as Stream;
            if (stream != null)
            {
                stream.CopyTo(writeStream);
                stream.Close();
            }
            
        }
        public override bool CanReadType(Type type)
        {
            return false;
        }

        public override bool CanWriteType(Type type)
        {
            return typeof(Stream) == type;
        }
    }
}
